<?php

/**
 * This is the model class for table "{{faq}}".
 *
 * The followings are the available columns in table '{{faq}}':
 * @property integer $faqId
 * @property string $faqType
 * @property string $faqQid
 * @property integer $faqModelId
 * @property string $faqTitle
 * @property string $faqContent
 * @property integer $faqStatus
 * @property string $faqUserName
 * @property string $faqUserEmail
 * @property integer $faqMostUseful
 */
class Ask extends CActiveRecord {

    public $verifyCode;

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{faq}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('faqModelId, faqBrandId, faqCategoryId, faqTitle, faqContent', 'required'),
            array('faqUserName, faqUserEmail', 'required', 'on' => 'model'),
            array('faqModelId, faqBrandId, faqCategoryId, faqTitle, faqContent, faqUserName, faqUserEmail', 'filter', 'filter' => array($obj = new CHtmlPurifier(), 'purify')),
//			array('faqId, faqModelId, faqStatus, faqMostUseful', 'numerical', 'integerOnly' => true),
//			array('faqType', 'length', 'max' => 8),
//			array('faqQid', 'length', 'max' => 10),
//			array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(), 'captchaAction'=>'main/ajax/captcha', 'on' => 'model'),
            //array('faqTitle, faqContent, faqUserName, faqUserEmail', 'length', 'max' => 1000),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('faqId, faqType, faqQid, faqModelId, faqTitle, faqContent, faqStatus, faqUserName, faqUserEmail, faqMostUseful, model', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'model' => 'sad',
            'faqId' => 'Faq',
            'faqType' => 'Faq Type',
            'faqQid' => 'Faq Qid',
            'faqModelId' => 'Модель',
            'faqTitle' => 'Вопрос',
            'faqContent' => 'Содержание',
            'faqStatus' => 'Статус',
            'faqUserName' => 'Имя пользователя',
            'faqUserEmail' => 'E-mail пользователя',
            'faqMostUseful' => 'Faq Most Useful',
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Faq the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord)
                $this->faqView = 0;
            $this->faqGlobalType = $this->faqGlobalType == 'model' ? 'model' : 'qa';
            foreach (Yii::app()->params['categories'] as $cat) {
                if (!empty($cat['faqUrl'])) {
                    $arr1[] = $cat['faqUrl'];
                    $arr2[] = $cat['pageUrl'];
                }
            }
            $url = $this->faqGlobalType == 'qa' ? 'qa/' . str_replace($arr2, $arr1, Page::model()->findByPk($this->faqModelId)->pageUrl) . '/' . getUrl::cleanString($this->faqTitle) : Page::model()->findByPk($this->faqModelId)->pageUrl;
            $this->faqCreateDate = time();
            $this->faqType = $this->faqType == 'answer' ? 'answer' : 'question';
            $this->faqUrl = $url;
            if (!Yii::app()->user->isGuest) {
                $this->faqUserName = Yii::app()->user->name['userName'];
                $this->faqUserEmail = Yii::app()->user->name['userEmail'];
            }
            $this->faqStatus = 0;
            return true;
        }
        return FALSE;
    }

}
